﻿using Sunboxsoft.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sunboxsoft.ASMS.Server.Domain.Service
{
    public class AuditingService
    {
        public ServiceResult ApproveStation(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Entity.EStation station = dbContext.Set<Entity.EStation>().SingleOrDefault(p => p.Id == id);
                if (station == null)
                {
                    return new ServiceResult(1, "station not exist");
                }
                station.Status = Constant.CommonStatus.Enabled;
                dbContext.Entry<Entity.EStation>(station).State = System.Data.Entity.EntityState.Modified;
                try
                {
                    dbContext.SaveChanges();
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("ApproveStation error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult ApproveStationMany(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for(int index = 0; index < idArray.Length; index++)
                    {
                        dbContext.Database.ExecuteSqlCommand("update ASMS_Station set [Status]={0} where [Id]={1}", Constant.CommonStatus.Enabled, idArray[index]);
                    }
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("ApproveStationMany error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult RejectStation(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                Entity.EStation station = dbContext.Set<Entity.EStation>().SingleOrDefault(p => p.Id == id);
                if (station == null)
                {
                    return new ServiceResult(1, "station not exist");
                }
                station.Status = Constant.CommonStatus.Rejected;
                dbContext.Entry<Entity.EStation>(station).State = System.Data.Entity.EntityState.Modified;
                try
                {
                    dbContext.SaveChanges();
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("RejectStation error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult RejectStationMany(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                    {
                        dbContext.Database.ExecuteSqlCommand("update ASMS_Station set [Status]={0} where [Id]={1}", Constant.CommonStatus.Rejected, idArray[index]);
                    }
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("ApproveStationMany error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult ApproveSupplier(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_Supplier set [Status]={0} where [Id]={1}", Constant.CommonStatus.Enabled, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("ApproveSupplier error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult ApproveSupplierMany(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                        dbContext.Database.ExecuteSqlCommand("update ASMS_Supplier set [Status]={0} where [Id]={1}", Constant.CommonStatus.Enabled, idArray[index]);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("ApproveSupplierMany error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult RejectSupplier(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_Supplier set [Status]={0} where [Id]={1}", Constant.CommonStatus.Rejected, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("RejectSupplier error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult RejectSupplierMany(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                        dbContext.Database.ExecuteSqlCommand("update ASMS_Supplier set [Status]={0} where [Id]={1}", Constant.CommonStatus.Rejected, idArray[index]);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("RejectSupplierMany error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult ApproveServiceItem(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_ServiceItem set [Status]={0} where [Id]={1}", Constant.CommonStatus.Enabled, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("ApproveServiceItem error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult ApproveServiceItemMany(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                        dbContext.Database.ExecuteSqlCommand("update ASMS_ServiceItem set [Status]={0} where [Id]={1}", Constant.CommonStatus.Enabled, idArray[index]);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("ApproveServiceItemMany error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult RejectServiceItem(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_ServiceItem set [Status]={0} where [Id]={1}", Constant.CommonStatus.Rejected, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("RejectServiceItem error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult RejectServiceItemMany(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                        dbContext.Database.ExecuteSqlCommand("update ASMS_ServiceItem set [Status]={0} where [Id]={1}", Constant.CommonStatus.Rejected, idArray[index]);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("RejectServiceItemMany error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }


        public ServiceResult ApproveMaterial(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_Material set [Status]={0} where [Id]={1}", Constant.CommonStatus.Enabled, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("ApproveMaterial error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult ApproveMaterialMany(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                        dbContext.Database.ExecuteSqlCommand("update ASMS_Material set [Status]={0} where [Id]={1}", Constant.CommonStatus.Enabled, idArray[index]);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("ApproveMaterialMany error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult RejectMaterial(Guid id)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    dbContext.Database.ExecuteSqlCommand("update ASMS_Material set [Status]={0} where [Id]={1}", Constant.CommonStatus.Rejected, id);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("RejectMaterial error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }

        public ServiceResult RejectMaterialMany(Guid[] idArray)
        {
            using (ASMSDbContext dbContext = new ASMSDbContext())
            {
                try
                {
                    for (int index = 0; index < idArray.Length; index++)
                        dbContext.Database.ExecuteSqlCommand("update ASMS_Material set [Status]={0} where [Id]={1}", Constant.CommonStatus.Rejected, idArray[index]);
                    return new ServiceResult();
                }
                catch (Exception ex)
                {
                    Logger.ErrorToTag(Library.LogTag, string.Format("RejectMaterialMany error,ex:{0}", ex));
                    return new ServiceResult(1, ex.Message);
                }
            }
        }
    }
}
